package com.gt.web.srv.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gt.web.api.common.R;
import com.gt.web.api.exception.BusinessException;
import com.gt.web.srv.domain.RepairApplication;
import com.gt.web.srv.domain.SsParticipants;
import com.gt.web.srv.mapper.SsParticipantsMapper;
import com.gt.web.srv.mapstructs.RepairApplicationStruct;
import com.gt.web.srv.mapstructs.SsParticipantsStruct;
import com.gt.web.srv.service.SsActivityService;
import com.gt.web.srv.service.SsParticipantsService;
import com.gt.web.srv.web.api.command.SsParticipantsCommand;
import com.gt.web.srv.web.api.resource.ss.RepairApplicationResource;
import com.gt.web.srv.web.api.resource.ss.SsParticipantsResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author jh
 * @Date 2024/5/16 21:49
 * @PackageName:com.gt.web.srv.service.impl
 * @ClassName: ArticleServiceImpl
 * @Description: TODO
 */
@Service
public class SsParticipantsServiceImpl  implements SsParticipantsService {

    @Autowired
    private SsParticipantsMapper ssParticipantsMapper;
    @Autowired
    private SsActivityService ssActivityService;

    @Override
    public SsParticipantsResource getById(Long ssParticipantsId) {
        return SsParticipantsStruct.INSTANCE.simple(ssParticipantsMapper.selectById(ssParticipantsId));
    }

    @Override
    public PageInfo<SsParticipantsResource> page(SsParticipantsCommand request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<RepairApplication> repairApplications = ssParticipantsMapper.list(request);
        List<SsParticipantsResource> repairApplicationResources = SsParticipantsStruct.INSTANCE.simpleList(repairApplications);
        return new PageInfo<>(repairApplicationResources);
    }

    @Override
    public SsParticipants getByDocumentNumber(String documentNumber) {
        return ssParticipantsMapper.selectByDocumentNumber(documentNumber);
    }

    @Override
    public SsParticipants getByPhone(String phone) {
        return ssParticipantsMapper.selectByPhone(phone);
    }

    @Override
    public List<SsParticipants> getByStatus(Integer status) {
        return ssParticipantsMapper.selectByStatus(status);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SsParticipantsCommand dto) {
        SsParticipants ssParticipants = SsParticipantsStruct.INSTANCE.simple(dto);
        if (ssParticipants.getSsParticipantsId() == null) {
            // 校验当前用户是否已报名
            List<SsParticipants> list = ssParticipantsMapper.selectByActivityId(ssParticipants.getActivityId(),ssParticipants.getUserId());
            if (CollectionUtil.isNotEmpty(list)) {
                throw new BusinessException(2010,"当前用户已报名活动！");
            }
            // 新增
            ssParticipantsMapper.insert(ssParticipants);
            // 增加报名人数
            Boolean aBoolean = ssActivityService.increaseEnrollNum(ssParticipants.getActivityId(), 1);
            if (!aBoolean) {
                throw new BusinessException(2010,"更新报名人数失败！");
            }
        } else {
            // 更新
            ssParticipantsMapper.update(ssParticipants);
        }
    }

    @Override
    public boolean updateStatus(Long ssParticipantsId, Integer status) {
        return ssParticipantsMapper.updateStatus(ssParticipantsId, status) > 0;
    }

    @Override
    public boolean delete(Long ssParticipantsId) {
        return ssParticipantsMapper.deleteById(ssParticipantsId) > 0;
    }

}
